﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Project;
using AMM.Model;
using AMM.Model.Project;

namespace AMM.Business.Project {
    public class MouldProcessor : BaseProcessor {

        private int _pageSize;
        private MouldRepository _mouldRepository;

        public MouldProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._mouldRepository = new MouldRepository(pageSize);
        }

        public Mould FetchMouldById(int mouldId) {
            try {
                return this._mouldRepository.FetchMouldById(mouldId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定模具失败！");
            }
        }

        public List<MouldDetailsViewModel> FetchMouldDetailsViewModelList(int mouldId) {
            try {
                return this._mouldRepository.FetchMouldByIdForView(mouldId).MouldDetails.Select(x => {
                    return new MouldDetailsViewModel() {
                        Key = x.MouldDetailsId,
                        Description = x.Description,
                        MaterialCode = x.Material.Code,
                        MouldHoleAmount = x.MouldHoleAmount,
                        MouldHoleStatus = x.Basic.Code,
                        Name = x.Name,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定模具失败！");
            }
        }

        public List<MouldViewModel> FetchMouldViewModelList(MouldSearchModel searchModel, int pageIndex = 1) {
            try {
                List<Mould> resultList = this._mouldRepository.FetchMouldList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new MouldViewModel() {
                        Key = x.MouldId,
                        Code = x.Code,
                        Name = x.Name,
                        Description = x.Description,
                        MouldFrameworkCode = x.MouldFrameworkCode,
                        ProductType = x.ProductType,
                        MouldType = x.Basic.Code,
                        VendorPlace = x.Vendor.Code,
                        FinishDate = x.FinishDate,
                        Factory = x.Factory,
                        Owner = x.Owner,
                        Cost = x.Cost,
                        PayOff = x.PayOff,
                        LastMoveDate = x.LastMoveDate,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取模具列表失败！");
            }
        }

        public void Insert(Mould mould) {
            try {
                mould.MouldId = Guid.NewGuid().GetHashCode();
                mould.CreatedDatetime = DateTime.Now;
                mould.MouldDetails.ToList().ForEach(x => {
                    x.CreatedBy = mould.CreatedBy;
                    x.CreatedDatetime = mould.CreatedDatetime;
                    x.MouldDetailsId = Guid.NewGuid().GetHashCode();
                });
                this._mouldRepository.Insert(mould);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新模具失败！");
            }
        }

        public void Update(Mould mould) {
            try {
                Mould oldMould = this.FetchMouldById(mould.MouldId);
                if (!oldMould.RowVersion.SequenceEqual(mould.RowVersion)) {
                    throw this.BuildException("更新模具失败，数据不一致性错误！");
                }
                mould.ModifiedDatetime = DateTime.Now;
                this._mouldRepository.UpdateMould(mould, oldMould);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新模具失败！");
            }
        }

        public void Delete(Mould mould) {
            try {
                this._mouldRepository.Delete(mould);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除模具失败！");
            }
        }

        public void Delete(params int[] mouldIds) {
            try {
                this._mouldRepository.DeleteMultipleMould(mouldIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除模具失败！");
            }
        }
    }
}
